package zzs.generalframework.console.core.parameter.commonparameter.recordstatus.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zzs.generalframework.common.core.parameter.commonparameter.recordstatus.RecordStatusBO;
import zzs.generalframework.common.core.parameter.commonparameter.recordstatus.RecordStatusInfo;
import zzs.generalframework.common.core.parameter.commonparameter.recordstatus.RecordStatusPO;
import zzs.generalframework.console.common.exception.ConsoleException;
import zzs.generalframework.console.common.exception.ConsoleRollbackException;
import zzs.generalframework.console.common.model.ConsoleReturnBO;
import zzs.generalframework.console.common.model.ConsoleReturnBO.ConsoleResult;
import zzs.generalframework.console.core.parameter.commonparameter.recordstatus.dao.IRecordStatusDao;
import zzs.generalframework.console.core.parameter.commonparameter.recordstatus.service.IRecordStatusService;
import zzs.generalframework.util.core.reflect.BeanUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.parameter.commonparameter.recordstatus.service.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
@Service("zzs.generalframework.console.core.parameter.commonparameter.recordstatus.service.impl.recordStatusServiceImpl")
public class RecordStatusServiceImpl implements IRecordStatusService
{

	private Logger logger = LoggerFactory.getLogger(RecordStatusServiceImpl.class);
	@Autowired
	BeanUtils beanUtils;
	@Autowired
	IRecordStatusDao recordStatusDao;

	@Override
	public ConsoleReturnBO saveRecordStatus(RecordStatusBO recordStatusBO)
	{
		logger.debug("====保存记录状态信息开始====");
		logger.debug("====保存记录状态信息.入参:RecordStatusBO=[{}]====", recordStatusBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		RecordStatusPO recordStatusPO = new RecordStatusPO();
		copyRecordStatusBO2RecordStatusPO(recordStatusBO, recordStatusPO);
		Boolean insertResult = recordStatusDao.insert(recordStatusPO);
		if (insertResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====保存记录状态信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存记录状态信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO saveRecordStatus(List<RecordStatusBO> recordStatusBOList) throws ConsoleException
	{
		logger.info("====保存记录状态信息列表开始====");
		logger.debug("====保存记录状态信息列表.入参:RecordStatusBOList=[{}]====", recordStatusBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (RecordStatusBO itemBO : recordStatusBOList)
		{
			ConsoleReturnBO itemResult = saveRecordStatus(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====保存记录状态信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存记录状态信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO getRecordStatus(RecordStatusInfo recordStatusInfo)
	{
		logger.info("====获取记录状态信息开始====");
		logger.debug("====获取记录状态信息.入参:RecordStatusInfo=[{}]====", recordStatusInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<RecordStatusPO> recordStatusPOList = recordStatusDao.select(recordStatusInfo);
		if (recordStatusPOList == null || recordStatusPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else if (recordStatusPOList.size() == 1)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			RecordStatusBO recordStatusBO = new RecordStatusBO();
			copyRecordStatusPO2RecordStatusBO(recordStatusPOList.get(0), recordStatusBO);
			returnConsoleReturnBO.setReturnObj(recordStatusBO);
		}
		else
		{
			returnConsoleReturnBO.setMessage("记录不唯一");
			RecordStatusBO recordStatusBO = new RecordStatusBO();
			copyRecordStatusPO2RecordStatusBO(recordStatusPOList.get(0), recordStatusBO);
			returnConsoleReturnBO.setReturnObj(recordStatusBO);
		}
		logger.debug("====获取记录状态信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====获取记录状态信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO listRecordStatus(RecordStatusInfo recordStatusInfo)
	{
		logger.info("====获取记录状态信息列表开始====");
		logger.debug("====获取记录状态信息列表.入参:RecordStatusInfo=[{}]====", recordStatusInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<RecordStatusPO> recordStatusPOList = recordStatusDao.select(recordStatusInfo);
		if (recordStatusPOList == null || recordStatusPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			List<RecordStatusBO> resultList = new ArrayList<RecordStatusBO>();
			for (RecordStatusPO itemPO : recordStatusPOList)
			{
				RecordStatusBO itemBO = new RecordStatusBO();
				copyRecordStatusPO2RecordStatusBO(itemPO, itemBO);
				resultList.add(itemBO);
			}
			returnConsoleReturnBO.setReturnObj(resultList);
		}
		logger.debug("====获取记录状态信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====获取记录状态信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateRecordStatus(RecordStatusBO recordStatusBO)
	{
		logger.info("====更新记录状态信息开始====");
		logger.debug("====更新记录状态信息.入参:RecordStatusBO=[{}]====", recordStatusBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		RecordStatusPO recordStatusPO = new RecordStatusPO();
		copyRecordStatusBO2RecordStatusPO(recordStatusBO, recordStatusPO);
		Boolean updateResult = recordStatusDao.update(recordStatusPO);
		if (updateResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====更新记录状态信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新记录状态信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateRecordStatus(List<RecordStatusBO> recordStatusBOList) throws ConsoleException
	{
		logger.info("====更新记录状态信息列表开始====");
		logger.debug("====更新记录状态信息列表.入参:List<RecordStatusBO>=[{}]====", recordStatusBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (RecordStatusBO itemBO : recordStatusBOList)
		{
			ConsoleReturnBO itemResult = updateRecordStatus(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====更新记录状态信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新记录状态信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeRecordStatus(RecordStatusBO recordStatusBO)
	{
		logger.info("====删除记录状态信息开始====");
		logger.debug("====删除记录状态信息.入参:RecordStatusBO＝[{}]======", recordStatusBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		RecordStatusPO recordStatusPO = new RecordStatusPO();
		copyRecordStatusBO2RecordStatusPO(recordStatusBO, recordStatusPO);
		Boolean deleteResult = recordStatusDao.delete(recordStatusPO);
		if (deleteResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====删除记录状态信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====删除记录状态信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeRecordStatus(List<RecordStatusBO> recordStatusBOList) throws ConsoleException
	{
		logger.info("====删除记录状态信息列表开始====");
		logger.debug("====删除记录状态信息列表.入参:List<RecordStatusBO>＝[{}]======", recordStatusBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (RecordStatusBO itemBO : recordStatusBOList)
		{
			ConsoleReturnBO itemResult = removeRecordStatus(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====删除记录状态信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====删除记录状态信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public Long countRecordStatus(RecordStatusBO recordStatusBO)
	{
		logger.info("====统计记录状态信息开始====");
		logger.debug("====统计记录状态信息。入参：RecordStatusBO＝[{}]======", recordStatusBO);
		Long returnLong = new Long(0);
		RecordStatusPO recordStatusPO = new RecordStatusPO();
		copyRecordStatusBO2RecordStatusPO(recordStatusBO, recordStatusPO);
		returnLong = recordStatusDao.count(recordStatusPO);
		logger.debug("====统计记录状态信息.出参:Long＝[{}]======", returnLong);
		logger.info("====统计记录状态信息结束====");
		return returnLong;
	}

	private void copyRecordStatusBO2RecordStatusPO(RecordStatusBO recordStatusBO, RecordStatusPO recordStatusPO)
	{
		beanUtils.copyBeanNotNull(recordStatusBO, recordStatusPO);
		if (recordStatusBO.getSysID() != null)
		{
			recordStatusPO.setSeqID(recordStatusBO.getSysID());
		}
	}

	private void copyRecordStatusPO2RecordStatusBO(RecordStatusPO recordStatusPO, RecordStatusBO recordStatusBO)
	{
		beanUtils.copyBeanNotNull(recordStatusPO, recordStatusBO);
		if (recordStatusPO.getSeqID() != null)
		{
			recordStatusBO.setSysID(recordStatusPO.getSeqID());
		}
	}

}
